﻿/* Copyright (c) 2010 Lexalytics Inc.

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace LexaScopeNET
{
    /// <summary>
    /// Static class encapsulating available entity types
    /// </summary>
    public static class LXAEntityType
    {
        /// <summary>Enumeration of entity types</summary>
        public enum Types
        {
            /// <summary>Person entity</summary>
            Person,
            /// <summary>Company entity</summary>
            Company,
            /// <summary>Place entity</summary>
            Place,
            /// <summary>Product entity</summary>
            Product
        }
    }

    /// <summary>
    /// Encapsulates an entity and it's mentions
    /// </summary>
    public class LXAEntity
    {
        /// <summary>Entity text</summary>
        public string Entity = "";
        /// <summary>Number of entity mentions</summary>
        public int Mentions = 0;

        public float NeutralLowerBound = 0f;
        public float NeutralUpperBound = 0f;

        private double _pnratio = 0.0;
        /// <summary>Ratio of positive to negative mentions</summary>
        public double PNRatio { get { return CalculatePNRatio(); } }
        /// <summary>List of assets with entity mentions</summary>
        public List<LXAEntityMention> MentionList;

        /// <summary>
        /// Default constructor
        /// </summary>
        public LXAEntity()
        {
            MentionList = new List<LXAEntityMention>();
        }

        /// <summary>
        /// Private method to calculate PN ratio for public property
        /// </summary>
        /// <returns>Double precision value for PN ratio</returns>
        private double CalculatePNRatio()
        {
            if (_pnratio == 0.0)
            {
                double pos = 0.1;
		        double neg = 0.1;
        		foreach(LXAEntityMention aMention in MentionList)
		        {			
			        if ( (aMention.Sentiment < NeutralLowerBound) && (aMention.Mentions > 0) )
			        {
                        neg++;
			        }
			        else if ( (aMention.Sentiment > NeutralUpperBound) && (aMention.Mentions > 0) )
			        {
                        pos++;
			        }
#if DEBUG
                    System.Diagnostics.Trace.WriteLine(String.Format("{0}\t{1}\t{2}", aMention.Magnitude, aMention.Sentiment, aMention.Mentions));
#endif
                }
		
		        if (pos < neg)
		        {
			        _pnratio = -1.0 * (Convert.ToDouble(neg) / Convert.ToDouble(pos));
		        }
                else if (neg < pos)
                {
                    _pnratio = (Convert.ToDouble(pos) / Convert.ToDouble(neg));
                }
                else
                {
                    _pnratio = 0.0;
                }

		        if (_pnratio > 10.0)
		        {
			        _pnratio = 10.0;
		        }
		        else if (_pnratio < -10.0)
		        {
			        _pnratio = -10.0;
		        }

            }
            return _pnratio;
        }
    }
}
